<template>
    <div class="comment">
        <div class='comment-product' :style="{height:clientHeight+'rem'}">
            <div class="comment-head">
                <div class="avatar-box">
                    <img :src="userInfo['headimgurl']" alt="">
                </div>
                <div class="comment-star">
                    <p>描述相符</p>
                    <div class="stars">
                        <div class="stars-box" v-for="(item,index) in 5" :key="index" @click="chooseStar(index)">
                            <img src="~assets/icons/star_orange.png" alt="" v-show="index <= starNum">
                            <img src="~assets/icons/star_empty.png" alt="" v-show="index > starNum">
                        </div>
                    </div>
                </div>
            </div>
            <div class="comment-content">
                <mt-field placeholder="宝贝满足你的期待吗？说说你的使用心得，分享给想买的他们吧" type="textarea" rows="4" v-model="comments.introduction"></mt-field>
            </div>
            <div class="upload-images">
                <div class="upload-box" ref="uploadBox">
                    <div class="upload-img">
                        <input type="file" accept="image/*" @change="getImage($event)">
                        <div class="add-img">
                            <img src="~assets/icons/camera.png" alt="">
                            <span>添加图片</span>
                        </div>
                    </div>
                </div>
                <img class="user-upload-img" :src="uploadImg" alt="">
            </div>
            
            <div class="upload-btn" @click="saveComment">
                <span>确认发布</span>
            </div>
        </div>
    </div>
</template>

<script>
import { Field, Toast, Indicator } from 'mint-ui';

export default {
    name:'comment',
    data(){
        return {
            starNum:-1,
            comments:{
                introduction:''
            },
            clientHeight:0,
            formData:null,
            uploadImg:'',
            reader:null,
            userInfo:{}
        }
    },
    methods:{
        chooseStar(index){
            this.starNum = index;
        },
        getImage(ev){
            let that = this;
            this.formData.append('imgurl', ev.target.files[0]);
            let file = ev.target.files[0];
            let reader = new FileReader();
            reader.readAsDataURL(file);
            let img = new Image();
            this.reader = reader;
            reader.onload = function(){ 
                that.uploadImg = this.result;
            }
        },
        saveComment(){
            if(this.comments.introduction == ''){
                this.toast('请填写您对产品的评价！');
                return;
            }
            let that = this;
            let token = localStorage.getItem('token');
            let orderId = this.$route.params.orderId;
            console.log(orderId);
            let productId = this.$route.params.productId;
            this.formData.append('order_num', orderId);
            this.formData.append('goods_id', productId);
            this.formData.append('degree', this.starNum);
            this.formData.append('content', this.comments.introduction);
            Indicator.open({
                text: '发表中...',
                spinnerType: 'fading-circle'
            })
            that.$ajax(that, 'post', that.$api.saveComment, this.formData, token, function(res){
                console.log(res);
                if(res.data['error_code'] == 0){
                    Indicator.close();
                    that.toast('评论发表成功！');
                    that.$router.push('/container/home');
                }else{
                    that.toast(res.data['msg'])
                }
            })
        },
        getUserInfo(){
            let that = this;
            let token = localStorage.getItem('token');
            let loader = that.loading();
            that.$ajax(that, 'get', that.$api.userInfo, null, token, function(res){
                if(res.data['error_code'] == 0){
                    that.userInfo = res.data.msg;
                }
                loader.close();
            })
        }
    },
    mounted(){
        this.formData = new FormData();
        this.$store.state.tab = '发布评论';
        let scale = window.devicePixelRatio;
        let clientHeight = document.documentElement.clientHeight;
        this.clientHeight = (scale*clientHeight)/100;
        console.log(this.$route);
    },
    activated(){
        this.$store.state.tab = '发布评论';
        this.getUserInfo();
    },
    components:{
        Field,
        Toast,
        Indicator
    }
}
</script>

<style lang='less'>
@rem:100rem;

.comment {
    width: 750/@rem;
    margin-top: 88/@rem;
    .mint-field.is-textarea .mint-cell-value{
        width: 100%;
        box-sizing: border-box;
    }
    .comment-product{
        width: 100%;
        background: #ffffff;
        .comment-head{
            height: 100/@rem;
            display: flex;
            align-items: center;
            border-bottom: 2/@rem solid #f0f0f0;
            .avatar-box{
                width: 60/@rem;
                height: 60/@rem;
                margin-left: 20/@rem;
                img{
                    width: 60/@rem;
                    height: 60/@rem;
                }
            }
            .comment-star{
                display: flex;
                align-items: center;
                p{
                    margin-left: 20/@rem;
                }
                .stars{
                    display: flex;
                    margin-left: 35/@rem;
                    img{
                        width: 32/@rem;
                        height: 32/@rem;
                        margin: 0 10/@rem;
                    }
                }
            }
        }
        .comment-content{
            font-family: 'Microsoft Yahei';
            textarea{
                width: 750/@rem;
            }
            .mint-field-core{
                font-size: 26/@rem;
                font-family: 'Microsoft Yahei';
            }
        }
        .upload-images{
            width: 100%;
            box-sizing: border-box;
            padding: 0 20/@rem;
            display: flex;
            justify-content: flex-start;
            align-items: center;
            .user-upload-img{
                width: 160/@rem;
                margin-left: 20/@rem;
            }
        }
        .upload-box{
            width: 162/@rem;
            height: 162/@rem;
        }
        .upload-img{
            width: 160/@rem;
            height: 160/@rem;
            position: relative;
            display: flex;
            justify-content: flex-start;
            input{
                width: 160/@rem;
                height: 160/@rem;
                z-index: 999;
                opacity: 0;
            }
            .add-img{
                width: 160/@rem;
                height: 160/@rem;
                position: absolute;
                display: flex;
                flex-direction: column;
                align-items: center;
                justify-content: center;
                border: 2/@rem solid #bfbfbf;
                img{
                    width: 60/@rem;
                    height: 48/@rem;
                }
                span{
                    font-size: 24/@rem;
                    color: #bfbfbf;
                }
            }
        }
        .upload-btn{
            width: 710/@rem;
            height: 70/@rem;
            line-height: 70/@rem;
            margin: 0 auto;
            margin-top: 80/@rem;
            background: #6699ff;
            border-radius: 10/@rem;
            span{
                font-size: 30/@rem;
                color: #ffffff;
            }
        }
    }
}
</style>
